package com.judian.smart.util;

import android.text.TextUtils;

import com.judian.smart.R;
import com.judian.smart.base.MyApplication;
import com.judian.smart.model.ServiceInfo;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ServiceUtils {

    public static ServiceInfo serviceInfo = null;
    private static File jsonFile = new File(MyApplication.getInstance()
            .getRootPath() + File.separator + "service_json");

    public static ServiceInfo getServiceInfo() {
        if (serviceInfo == null) {
            String json = JSONFileUtils.getJsonStringFromFile(jsonFile)
                    .replace("\r\n", "");
            if (TextUtils.isEmpty(json)) {
                serviceInfo = new ServiceInfo();
                serviceInfo.setServiceName("创先服务器");
                serviceInfo.setIP("112.74.56.95");
                serviceInfo.setPort(2020);
                saveServiceInfo();
            } else {
                try {
                    JSONObject object = new JSONObject(json);
                    serviceInfo = new ServiceInfo();
                    serviceInfo.setServiceName(object.getString("ServiceName"));
                    serviceInfo.setIP(object.getString("IP"));
                    serviceInfo.setPort(object.getInt("Port"));
                } catch (Exception e) {
                    serviceInfo = new ServiceInfo();
                    serviceInfo.setServiceName(MyApplication.getInstance().getString(R.string.servicehly));
                    serviceInfo.setServiceName("创先服务器");
                    serviceInfo.setIP("112.74.56.95");
                    serviceInfo.setPort(2020);
                    saveServiceInfo();
                }
            }
        }
        return serviceInfo;
    }

    public static void saveServiceInfo() {
        try {
            if (serviceInfo != null) {
                JSONObject bject = new JSONObject();
                bject.put("ServiceName", serviceInfo.getServiceName());
                bject.put("IP", serviceInfo.getIP());
                bject.put("Port", serviceInfo.getPort());
                JSONFileUtils.saveJSONToFile(jsonFile, bject.toString());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static List<ServiceInfo> getServiceList() {
        List<ServiceInfo> list = new ArrayList<ServiceInfo>();
        ServiceInfo serviceInfo = new ServiceInfo();

        ServiceInfo serviceInfo1 = new ServiceInfo();
        serviceInfo1.setServiceName(MyApplication.getInstance().getString(R.string.servicehly));
        serviceInfo1.setIP("112.74.56.95");
        serviceInfo1.setPort(2020);
        list.add(serviceInfo1);


        ServiceInfo serviceInfo4 = new ServiceInfo();
        serviceInfo4.setServiceName("自訂義");
        serviceInfo4.setIP("192.168.2.116");
        serviceInfo4.setPort(2020);
        list.add(serviceInfo4);
        return list;

    }
}
